package com.hntxk.cqzd.flup.service.impl;


import com.hntxk.core.framework.model.ResponseModel;
import com.hntxk.core.framework.model.ResultModel;
import com.hntxk.core.framework.service.impl.BaseServiceImpl;
import com.hntxk.core.util.StringUtil;
import com.hntxk.cqzd.common.service.CommonFileService;
import com.hntxk.cqzd.common.util.CalculateAge;
import com.hntxk.cqzd.diacrisis.entity.TblDiacrisisInfo;
import com.hntxk.cqzd.diacrisis.service.DiacrisisInfoService;
import com.hntxk.cqzd.flup.entity.FlupFile;
import com.hntxk.cqzd.flup.entity.FlupInfo;
import com.hntxk.cqzd.flup.model.FlupAndDiacModel;
import com.hntxk.cqzd.flup.model.FlupInfoAndModel;
import com.hntxk.cqzd.flup.service.FlupFileService;
import com.hntxk.cqzd.flup.service.FlupInfoService;
import com.hntxk.sp.dict.manager.DictManager;
import com.hntxk.sp.dict.model.Dict;
import com.hntxk.sp.log.model.ActionLog;
import com.hntxk.sp.log.model.ActionType;
import com.hntxk.sp.util.LogRecordUtil;
import com.hntxk.sp.util.LoginUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author <a href="huangbiquan@hotmail.com">Huang.Biquan</a>
 * @Description: 基本信息管理接口实现类 <br>
 * @Project: SP <br>
 * @date 2019/6/3 10:01 <br>
 */
@Component("flupInfoManageService")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class FlupInfoServiceImpl extends BaseServiceImpl<FlupInfo> implements FlupInfoService {

    final CommonFileService commonFileService;

    final DiacrisisInfoService diacrisisService;
    private TblDiacrisisInfo tblDiacrisisInfo;

    @Autowired
    @Qualifier("flupFileManageService")
    FlupFileService flupFileService;





    public static String FLUPINFO_MAPPER_NAMESPACE = "com.hntxk.cqzd.flup.mapper.FlupInfoMapper.";

    public FlupInfoServiceImpl(@Qualifier("commonFileServiceImpl") CommonFileService commonFileService, @Qualifier("diacInfoService") DiacrisisInfoService diacrisisService) {
        super(FLUPINFO_MAPPER_NAMESPACE);
        this.commonFileService = commonFileService;
        this.diacrisisService = diacrisisService;
    }

    /**
     * 新增随访
     * @param flupInfoAndModel
     * @param files
     * @return
     * @throws IOException
     */
    @Override
    public ResultModel<FlupInfoAndModel> insert(FlupInfoAndModel flupInfoAndModel, MultipartFile[] files) throws IOException {
        FlupAndDiacModel flupAndDiacModel=flupInfoAndModel.getFlupAndDiacModel();
        setSelectName(flupAndDiacModel);
        flupAndDiacModel.setCreateUserId(LoginUserUtil.getLoginOperatorIdLong());
        flupAndDiacModel.setCreateUserName(LoginUserUtil.getLoginUserName());
        flupAndDiacModel.setCreateTime(new Date());
        //新增随访信息
        insert(FLUPINFO_MAPPER_NAMESPACE+"insert",flupAndDiacModel);
        if(files!=null&&files.length>0){
            //调用上传文件的方法
            commonFileService.upload(FlupFileServiceImpl.FLUPFILE_MAPPER_NAMESPACE+"insert",files,flupAndDiacModel.getFlupId(),"flup");
        }
            //记录日志
        tblDiacrisisInfo= diacrisisService.getById(flupAndDiacModel.getDiacrisisId());
            Long aLong = LogRecordUtil.addLog(new ActionLog(FlupInfo.class, flupAndDiacModel.getFlupId() + "", "12",
                    ActionType.ADD, "新增随访信息：" + flupAndDiacModel.getFlupId() + "[ " + tblDiacrisisInfo.getPatientName() + " ]"));
        return ResultModel.empty();
    }

    /**
     * 设置下拉的name值和主键值
     * @param flupAndDiacModel
     */
    public void setSelectName(FlupAndDiacModel flupAndDiacModel){
        getPrimaryKey(flupAndDiacModel);
        if (flupAndDiacModel.getGestResultId()!=null) {
            Dict gestResultDict = DictManager.getInstance().getDict("CQ_BASE_GEST_RESULT", flupAndDiacModel.getGestResultId());
            //设置妊娠结局下拉name
            flupAndDiacModel.setGestResultName(gestResultDict.getText());
        }

        if (flupAndDiacModel.getInducedCauseId()!=null) {
            Dict inducedCauseDict = DictManager.getInstance().getDict("CQ_BASE_INDUCED_CAUSE", flupAndDiacModel.getInducedCauseId());
            //设置引产原因下拉name
            flupAndDiacModel.setInducedCauseName(inducedCauseDict.getText());
        }


        String specKeepId = flupAndDiacModel.getSpecKeepId();
        if(StringUtil.isNotNull(specKeepId)){
            String sprckeepName = CalculateAge.idToName(specKeepId, "CQ_BASE_SPEC_KEEP");
            flupAndDiacModel.setSpecKeepName(sprckeepName);

        }
    }



    @Override
    public ResultModel<FlupInfoAndModel> update(FlupInfoAndModel flupInfoAndModel, MultipartFile[] files) throws IOException {
        FlupAndDiacModel flupAndDiacModel=flupInfoAndModel.getFlupAndDiacModel();
        setSelectName(flupAndDiacModel);
        flupAndDiacModel.setUpdateTime(new Date());
        flupAndDiacModel.setUpdateUserId(LoginUserUtil.getLoginOperatorIdLong());
        flupAndDiacModel.setUpdateUserName(LoginUserUtil.getLoginUserName());
        if(files!=null&&files.length>0){
            //调用上传文件的方法
            commonFileService.upload(FlupFileServiceImpl.FLUPFILE_MAPPER_NAMESPACE+"insert",files,flupAndDiacModel.getFlupId(),"flup");
        }

         super.update(FLUPINFO_MAPPER_NAMESPACE + "update", flupAndDiacModel);
        LogRecordUtil.addLog(new ActionLog(FlupAndDiacModel.class, flupAndDiacModel.getDiacrisisId() + "", "12",
                ActionType.UPDATE, "修改随访信息：" + flupAndDiacModel.getDiacrisisId() + "[ " + flupAndDiacModel.getPatientName() + " ]"));
        return ResultModel.empty();
    }


    /**
     * 最新版修改随访信息
     * @param flupAndDiacModels
     * @return
     * @throws IOException
     */
    @Override
    public ResultModel<FlupAndDiacModel> insert(List<FlupAndDiacModel>  flupAndDiacModels) throws IOException {
        if (null!=flupAndDiacModels && flupAndDiacModels.size()>0){
            for (FlupAndDiacModel flupAndDiacModel : flupAndDiacModels) {
                setSelectName(flupAndDiacModel);
                flupAndDiacModel.setCreateUserId(LoginUserUtil.getLoginOperatorIdLong());
                flupAndDiacModel.setCreateUserName(LoginUserUtil.getLoginUserName());
                flupAndDiacModel.setCreateTime(new Date());
                //新增随访信息
                insert(FLUPINFO_MAPPER_NAMESPACE+"insert",flupAndDiacModel);
                if(flupAndDiacModel.getFiles()!=null&&flupAndDiacModel.getFiles().length>0){
                    //调用上传文件的方法
                    commonFileService.upload(FlupFileServiceImpl.FLUPFILE_MAPPER_NAMESPACE+"insert",flupAndDiacModel.getFiles(),flupAndDiacModel.getFlupId(),"flup");
                }
                //记录日志
                tblDiacrisisInfo= diacrisisService.getById(flupAndDiacModel.getDiacrisisId());
                Long aLong = LogRecordUtil.addLog(new ActionLog(FlupInfo.class, flupAndDiacModel.getFlupId() + "", "12",
                        ActionType.ADD, "新增随访信息：" + flupAndDiacModel.getFlupId() + "[ " + tblDiacrisisInfo.getPatientName() + " ]"));

            }

        }
                return ResultModel.empty();
    }

    /**
     * 最新版新增
     * @param flupAndDiacModels
     * @return
     * @throws IOException
     */
    @Override
    public ResultModel<FlupAndDiacModel> update(List<FlupAndDiacModel> flupAndDiacModels) throws IOException {
        if (null!=flupAndDiacModels && flupAndDiacModels.size()>0){
            delete(FLUPINFO_MAPPER_NAMESPACE+"deleteByDiacrisisId",flupAndDiacModels.get(0).getDiacrisisId());
            for (FlupAndDiacModel flupAndDiacModel : flupAndDiacModels) {
                setSelectName(flupAndDiacModel);
                flupAndDiacModel.setCreateUserId(LoginUserUtil.getLoginOperatorIdLong());
                flupAndDiacModel.setCreateUserName(LoginUserUtil.getLoginUserName());
                flupAndDiacModel.setCreateTime(new Date());
                flupAndDiacModel.setUpdateTime(new Date());
                flupAndDiacModel.setUpdateUserId(LoginUserUtil.getLoginOperatorIdLong());
                flupAndDiacModel.setUpdateUserName(LoginUserUtil.getLoginUserName());
                if(flupAndDiacModel.getFiles()!=null&&flupAndDiacModel.getFiles().length>0){
                    //调用上传文件的方法
                    commonFileService.upload(FlupFileServiceImpl.FLUPFILE_MAPPER_NAMESPACE+"insert",flupAndDiacModel.getFiles(),flupAndDiacModel.getFlupId(),"flup");
                }

                insert(FLUPINFO_MAPPER_NAMESPACE + "insert", flupAndDiacModel);
                LogRecordUtil.addLog(new ActionLog(FlupAndDiacModel.class, flupAndDiacModel.getDiacrisisId() + "", "12",
                        ActionType.UPDATE, "修改随访信息：" + flupAndDiacModel.getDiacrisisId() + "[ " + flupAndDiacModel.getPatientName() + " ]"));
            }
        }
        return ResultModel.empty();
    }

    @Override
    public void delete(String ids, boolean isCalled) {
        List<String> list = Arrays.asList(ids.split(","));
        List<FlupInfo> flupInfos=new ArrayList<>();
        for (String id : list) {
            flupInfos.add(get(Long.parseLong(id)));
            List<FlupFile> flupFiles = flupFileService.getByFkId(Long.parseLong(id));
            for(FlupFile flupFile:flupFiles){
                delete(FlupFileServiceImpl.FLUPFILE_MAPPER_NAMESPACE+"delete",flupFile);
            }
            delete(FLUPINFO_MAPPER_NAMESPACE+"delete",id);
        }
        for(FlupInfo flupInfo:flupInfos) {
            LogRecordUtil.addLog(new ActionLog(FlupInfo.class, flupInfo.getDiacrisisId() + "", "12",
                    ActionType.DELETE, "删除随访信息：" + flupInfo.getDiacrisisId() + "[ " + tblDiacrisisInfo.getPatientName() + " ]"));
        }

    }

    @Override
    public int countFileInfoById(FlupFile flupFile) {
        int count = super.count(FLUPINFO_MAPPER_NAMESPACE + "countFlupFile", flupFile);
        return count;
    }

    @Override
    public List<FlupInfoAndModel> getByFkId(Long id) {
        FlupInfo flupInfo=new FlupInfo();
        List<FlupInfoAndModel> flupInfoAndModels=new ArrayList<>();
        flupInfo.setDiacrisisId(id);
        List<FlupAndDiacModel> flupInfos = (List<FlupAndDiacModel>) query(FLUPINFO_MAPPER_NAMESPACE + "getByFkId", flupInfo);
        if(flupInfos.size()>0) {
            for (FlupAndDiacModel info : flupInfos) {
                FlupInfoAndModel flupInfoAndModel=new FlupInfoAndModel();
                flupInfoAndModel.setFlupAndDiacModel(info);
                List<FlupFile>  flupFiles= flupFileService.getByFkId(info.getFlupId());
                flupInfoAndModel.setFlupFiles(flupFiles);
                flupInfoAndModels.add(flupInfoAndModel);
            }
        }
        return flupInfoAndModels;
    }

    @Override
    public FlupInfoAndModel getById(Long id) {
        FlupInfoAndModel flupInfoAndModel=new FlupInfoAndModel();
        flupInfoAndModel.setFlupAndDiacModel(get(FLUPINFO_MAPPER_NAMESPACE+"get",id));
        flupInfoAndModel.setFlupFiles(flupFileService.getByFkId(id));
        return flupInfoAndModel;
    }

    @Override
    public ResponseModel getAllInfo(String param, String isValid) {
        FlupAndDiacModel flupAndDiacModel=new FlupAndDiacModel();
        if(param!=null&&param!=""){
            flupAndDiacModel.setPatientName(param);
        }
        if(isValid!=null&&isValid!=""){
            flupAndDiacModel.setIsValid(isValid.charAt(0));
        }

        return query2Json(FLUPINFO_MAPPER_NAMESPACE + "query", flupAndDiacModel, true);
    }

}
